﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net;
using System.IO;
using UnionnetClient.Model;

namespace UnionnetClient
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			controller = new Controller(this);
		}

		public ToolStripMenuItem ToolStripLogin
		{
			get { return tsmiLogin; }
		}

		public ToolStripMenuItem ToolStripLogout
		{
			get { return tsmiLogout; }
		}

		public NotifyIcon NotifyIcon
		{
			get { return notifyIcon1; }
		}

		public void SetToolStripEnable(ToolStripMenuItem item, bool b) 
		{
			if (this.contextMenuStrip1.InvokeRequired)
			{
				this.contextMenuStrip1.BeginInvoke(new SetPropertyHandler(_SetToolStripEnable), new object[] { item, b });
			}
			else
			{
				_SetToolStripEnable(item, b);
			}
		}

		#region 事件处理函数

		private void tsmiLogin_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
			if (tsmi.Text.Contains("登陆"))
			{
				controller.Login();
			}
			else if (tsmi.Text.Contains("取消登陆"))
			{
				UnloginStateWithUI us = controller.CurrentState as UnloginStateWithUI;
				if (us != null)
				{
					us.CancelLogin();
				}
			}
		}

		private void tsmiLogout_Click(object sender, EventArgs e)
		{
			controller.Logout();
		}

		private void tsmiSet_Click(object sender, EventArgs e)
		{
			Controller.Configure(new SettingsForm());
		}

		private void tsmiService_Click(object sender, EventArgs e)
		{
			Controller.GotoSelfService();
		}

		private void tsmiAbout_Click(object sender, EventArgs e)
		{
			new AboutForm().ShowDialog();
		}

		private void tsmiExit_Click(object sender, EventArgs e)
		{
			controller.Exit();
		}

		private void tsmiNetworkConfig_Click(object sender, EventArgs e)
		{
			new NetworkManager.MainForm().Show();
		}

		#endregion

		private Controller controller;

		private delegate void SetPropertyHandler(ToolStripMenuItem item, bool b);

		private void _SetToolStripEnable(ToolStripMenuItem item, bool b)
		{
			item.Enabled = b;
		}
	}
}